package com.oig.sys.auth.controller;


import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.auth.dto.TenantInfoDto;
import com.oig.sys.auth.service.ITbTenantInfoService;
import com.oig.sys.auth.vo.TenantInfoVo;
import com.oig.sys.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "1-租户管理")
@RestController
@RequestMapping(value = "/tenant")
@Slf4j
@RequiredArgsConstructor
public class TbTenantInfoController {

    private final ITbTenantInfoService tenantInfoService;


    @PreAuthorize("@pms.hasPermission('/tenant/addTenantInfo')")
    @SysLog(value="添加租户",apiUrl="/tenant/addTenantInfo")
    @Operation(summary = "11-添加租户")
    @PostMapping("/addTenantInfo")
    public TenantInfoVo addTenantInfo(@RequestBody @Validated(GroupAdd.class) TenantInfoDto tenantInfoDto){
        log.info("add 租户信息:{}", tenantInfoDto.toString());
        return tenantInfoService.addTenant(tenantInfoDto) ;
    }

    @PreAuthorize("@pms.hasPermission('/tenant/updateTenantInfo')")
    @SysLog(value="修改租户信息",apiUrl="/tenant/updateTenantInfo")
    @Operation(summary = "12-修改租户信息")
    @PostMapping("/updateTenantInfo")
    public TenantInfoVo updateTenantInfo(@RequestBody @Validated(GroupEdit.class) TenantInfoDto tenantInfoDto){
        log.info("add 租户信息:{}", tenantInfoDto.toString());
        return tenantInfoService.updateTenant(tenantInfoDto) ;
    }

    @PreAuthorize("@pms.hasPermission('/tenant/cancelTenantInfo')")
    @SysLog(value="逻辑删除租户信息",apiUrl="/tenant/cancelTenantInfo")
    @Operation(summary = "13-逻辑删除租户信息,逻辑删除租户信息")
    @PostMapping(value = "/cancelTenantInfo/{tenantId}")
    public SysResponse<Integer> cancelTenantInfo(@PathVariable("tenantId") Integer tenantId){
        log.info("cancel 租户信息{}", tenantId);
        this.tenantInfoService.cancelTenantInfo(tenantId) ;
        return SysResponse.ok(tenantId,"删除成功") ;
    }

    @PreAuthorize("@pms.hasPermission('/tenant/revertTenantInfo')")
    @SysLog(value="14-恢复租户信息",apiUrl="/tenant/revertTenantInfo")
    @Operation(summary = "恢复租户信息")
    @PostMapping(value = "/revertTenantInfo/{tenantId}")
    public SysResponse<Integer> revertTenantInfo(@PathVariable("tenantId") Integer tenantId){
        log.info("revert TenantInfo Id:{}",tenantId);
        this.tenantInfoService.revertTenantInfo(tenantId);
        return SysResponse.ok(tenantId,"恢复成功");
    }

    @Operation(summary = "10-查询所有租户")
    @PostMapping(value = "/findAllList")
    public List<TenantInfoVo> findAllList(){
        log.info("findTenantList");
        return this.tenantInfoService.findAllList();
    }


    @Operation(summary = "15-查看,查看详情，如果有子表则包含子表信息")
    @PostMapping(value = "/info/{tenantId}")
    public TenantInfoVo info(@PathVariable("tenantId") Integer tenantId){
        log.info("info TenantInfoVo id={}", tenantId) ;
        return this.tenantInfoService.info(tenantId);
    }

    @Operation(summary = "15-查看,查看详情，如果有子表则包含子表信息")
    @PostMapping("/getByCode/{code}")
    public TenantInfoVo getByCode(@PathVariable("code") String code){
        log.info("get TenantInfoVo by code={}", code) ;
        return this.tenantInfoService.getByCode(code);
    }




}
